
#ifndef __AUTO_LOCK_H
#define __AUTO_LOCK_H

#include "APRMutex.h"

/** An APRMutex automatic locking wrapper class. Destroying the
 * wrapped mutex afterwards is to be handled elsewhere. */
class APRAutoLock
{
   public:
      /** Lock the mutex.
       * 
       * @param i_mutex A pointer to a created APRMutex that is
       * unlocked. */
      APRAutoLock(APRMutex *i_mutex)
      {
         _mutex = i_mutex;
         _mutex->lock();
      }
      /** Unlock and remove the reference to the mutex. */
      ~APRAutoLock()
      {
         if (_mutex) _mutex->unlock();
      }
      /** Signal on the associated condition variable. */
      void signal()
      {
         _mutex->signal();
      }
      /** Wait on the associated condition variable. */
      void wait()
      {
         _mutex->wait();
      }
      /** This removes the lock and turns off the mutex before the
        * destructor. */
      void turnOff()
      {
         _mutex->unlock();
         _mutex = NULL;
      }
   private:
      /** A pointer to the APR mutex that is to be used. */
      APRMutex *_mutex;
};

#endif
